package xg.glycomaid;

import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.plot.CrosshairState;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleEdge;
import org.jfree.util.ShapeUtilities;




public class XYLineAndShapeRendererMod extends org.jfree.chart.renderer.xy.XYLineAndShapeRenderer{


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public XYLineAndShapeRendererMod(){
		super();
	}
	
	/**
	 * override
	 * 
     */
	
    protected void drawSecondaryPass(Graphics2D g2, XYPlot plot,
                                     XYDataset dataset,
                                     int pass, int series, int item,
                                     ValueAxis domainAxis,
                                     Rectangle2D dataArea,
                                     ValueAxis rangeAxis,
                                     CrosshairState crosshairState,
                                     EntityCollection entities) {

        Shape entityArea = null;

        // get the data point...
        double x1 = dataset.getXValue(series, item);
        double y1 = dataset.getYValue(series, item);

        if (java.lang.Double.isNaN(y1) || java.lang.Double.isNaN(x1)) {
            return;
        }

        PlotOrientation orientation = plot.getOrientation();
        RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
        RectangleEdge yAxisLocation = plot.getRangeAxisEdge();
        double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
        double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);

        if (getItemShapeVisible(series, item)) {
            Shape shape = getItemShape(series, item);
            if (orientation == PlotOrientation.HORIZONTAL) {
                shape = ShapeUtilities.createTranslatedShape(shape, transY1,
                        transX1);
            }
            else if (orientation == PlotOrientation.VERTICAL) {
                shape = ShapeUtilities.createTranslatedShape(shape, transX1,
                        transY1);
            }
            entityArea = shape;
            if (shape.intersects(dataArea)) {
                if (getItemShapeFilled(series, item)) {
                    if (super.getUseFillPaint()) {
                        g2.setPaint(getItemFillPaint(series, item));
                    }
                    else {
                        g2.setPaint(getItemPaint(series, item));
                    }
                    g2.fill(shape);
                }
                if (super.getDrawOutlines()) {
                    if (getUseOutlinePaint()) {
                        g2.setPaint(getItemOutlinePaint(series, item));
                    }
                    else {
                        g2.setPaint(getItemPaint(series, item));
                    }
                    g2.setStroke(getItemOutlineStroke(series, item));
                    g2.draw(shape);
                }
            }
        }

        double xx = transX1;
        double yy = transY1;
        if (orientation == PlotOrientation.HORIZONTAL) {
            xx = transY1;
            yy = transX1;
        }

        // draw the item label if there is one...
        if (isItemLabelVisible(series, item)) {
            drawItemLabel(g2, orientation, dataset, series, item, xx, yy, 
                    (y1 < 0.0));
        }

        int domainAxisIndex = plot.getDomainAxisIndex(domainAxis);
        int rangeAxisIndex = plot.getRangeAxisIndex(rangeAxis);
        updateCrosshairValues(crosshairState, x1, y1, domainAxisIndex,
                rangeAxisIndex, transX1, transY1, orientation);

        // add an entity for the item, but only if it falls within the data
        // area...
    
        if (entities != null && isPointInRect(dataArea, xx, yy ) && series!=0) {
        	addEntity(entities, creatRotatedRect(xx, yy), dataset, series, item, xx, yy);
        }
        if(entities != null && isPointInRect(dataArea, xx, yy ) && series==0){
        	addEntity(entities, entityArea, dataset, series, item, xx, yy);
        }
    }

    public Shape creatRotatedRect(double xx, double yy)
    {
    	Rectangle2D r = new Rectangle2D.Double(xx-5, yy-40, 10, 80);

    	AffineTransform at = AffineTransform.getRotateInstance(
    			Math.PI/4, r.getCenterX(), r.getCenterY());

    	Polygon p = new Polygon();

    	PathIterator i = r.getPathIterator(at);
    	while (!i.isDone()) {
    		double[] xy = new double[2];
    		i.currentSegment(xy);
    		p.addPoint((int) xy[0], (int) xy[1]);
    		i.next();
    	}
    
    	return p;
    }
}
